package com.example.service.serviceImpl;

import com.example.mapper.HistoryDataMapper;
import com.example.pojo.HumidityHistoryData;
import com.example.pojo.TemperatureHistoryData;
import com.example.service.HistoryDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class HistoryDataServiceImpl implements HistoryDataService {
    @Autowired
    private HistoryDataMapper historyDataMapper;

    /**
     * 获取温度历史数据
     */
    @Override
    public TemperatureHistoryData getTemperatureData(String token, String time) throws Exception {
        TemperatureHistoryData temperatureHistoryData = new TemperatureHistoryData();
        List<Map<String, Object>> mapList;
        switch (time) {
            //获取统计数据
            case "day" -> {
                LocalDateTime endTime = LocalDateTime.now();
                LocalDateTime startTime = endTime.minusHours(24);
                mapList = historyDataMapper.getTemperatureDataByHour(startTime, endTime);
            }
            case "week" -> {
                LocalDate endTime = LocalDate.now();
                LocalDate startTime = endTime.minusDays(7);
                mapList = historyDataMapper.getTemperatureDataByDay(startTime, endTime);
            }
            case "month" -> {
                LocalDate endTime = LocalDate.now();
                LocalDate startTime = endTime.minusDays(30);
                mapList = historyDataMapper.getTemperatureDataByDay(startTime, endTime);
            }
            case "year" -> {
                // 获取当前日期
                LocalDate endDate = LocalDate.now();
                // 获取一年前的日期
                LocalDate startDate = endDate.minusYears(1).plusMonths(1);
                mapList = historyDataMapper.getTemperatureDataByMonth(startDate, endDate);
            }
            default -> throw new Exception("Invalid parameter");
        }

        //组装结果
        temperatureHistoryData.setTimeList(mapList.stream().map(map -> map.get("time").toString()).toList());
        temperatureHistoryData.setDataList(mapList.stream().map(map -> map.get("data").toString()).toList());
        log.info("温度数据：{}", temperatureHistoryData);

        return temperatureHistoryData;
    }

    /**
     * 获取湿度历史数据
     */
    @Override
    public HumidityHistoryData getHumidityData(String token, String time) throws Exception {
        HumidityHistoryData humidityHistoryData = new HumidityHistoryData();
        List<Map<String, Object>> mapList;
        switch (time) {
            //获取统计数据
            case "day" -> {
                LocalDateTime endTime = LocalDateTime.now();
                LocalDateTime startTime = endTime.minusHours(24);
                mapList = historyDataMapper.getHumidityDataByHour(startTime, endTime);
            }
            case "week" -> {
                LocalDate endTime = LocalDate.now();
                LocalDate startTime = endTime.minusDays(7);
                mapList = historyDataMapper.getHumidityDataByDay(startTime, endTime);
            }
            case "month" -> {
                LocalDate endTime = LocalDate.now();
                LocalDate startTime = endTime.minusDays(30);
                mapList = historyDataMapper.getHumidityDataByDay(startTime, endTime);
            }
            case "year" -> {
                // 获取当前日期
                LocalDate endDate = LocalDate.now();
                // 获取一年前的日期
                LocalDate startDate = endDate.minusYears(1).plusMonths(1);
                mapList = historyDataMapper.getHumidityDataByMonth(startDate, endDate);
            }
            default -> throw new Exception("Invalid parameter");
        }

        //组装结果
        humidityHistoryData.setTimeList(mapList.stream().map(map -> map.get("time").toString()).toList());
        humidityHistoryData.setDataList(mapList.stream().map(map -> map.get("data").toString()).toList());

        return humidityHistoryData;
    }
}
